package com.nononsenseapps.feeder.mobile.ui.feed

import android.annotation.SuppressLint
import android.content.Intent
import android.os.*
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.leinardi.android.speeddial.SpeedDialActionItem
import com.nononsenseapps.feeder.db.ARG
import com.nononsenseapps.feeder.db.SYSTEM_FEED_LIST
import com.nononsenseapps.feeder.mobile.R
import com.nononsenseapps.feeder.mobile.ui.FeedActivity
import com.nononsenseapps.feeder.ui.FabClickListener
import com.nononsenseapps.feeder.ui.feed.AbstractFeedFragment
import com.nononsenseapps.feeder.util.*
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.launch

@InternalCoroutinesApi
@FlowPreview
class FeedFragment : AbstractFeedFragment() {

    @SuppressLint("UnsafeRepeatOnLifecycleDetector")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
           lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                initFab()
            }
        }
    }

    private fun initFab(){
        (activity as FeedActivity).initFab(R.drawable.ic_forward_top,com.nononsenseapps.feeder.ui.R.menu.feed_fab,1,object :FabClickListener{
            override fun fabOnClickListener() {
                when(emptyView.isVisible){
                    true->(activity as FeedActivity).openNavDrawer()
                    false-> recyclerView.smoothScrollToPosition(0)
                }
            }

            override fun fabOnDoubleClickListener() {
                globalViewModel.lastUndoReadItemID?.let{
                    feedItemsViewModel.markFeedItemAsUnRead(it)
                }
            }

            override fun fabOnLongClickListener() {
                when(emptyView.isVisible){
                    true->requestFeedSyncAll()
                    false-> {
                        adapter.getCurrentItemList().let{
                            markCurrentListAsReadAndSync(it)
                        }
                    }
                }
            }

            override fun onActionSelected(actionItem: SpeedDialActionItem?): Boolean {
                if (actionItem != null) {
                    onOptionsItemSelectedWithFab(actionItem.id)
                }
                return false
            }
        })
    }


    override fun openFeedsList() {
        (activity as FeedActivity).openNavDrawer()
    }

    override fun initLayoutManager() {
        if (TabletUtils.isTablet(activity)) {
           changeToStaggeredGridLayoutManager()
        }else
            super.initLayoutManager()
    }

    override fun getActivityIntent(): Intent =Intent(context, FeedActivity::class.java)


    override fun navigateToFeedFragment(id: Long, tag: String?) {
        findNavController().navigate(R.id.action_feedFragment_self, bundle {
            putLong(ARG.FEED_ID, id)
            tag?.let {putString(ARG.TAG, tag)}
        })
    }

    override fun navigateToSettingFragment() {
        findNavController().navigate(R.id.action_feedFragment_to_settingsFragment)
    }

    override fun navigateToFeedHotEditFragment() {
        findNavController().navigate(R.id.action_feedFragment_to_feedHotEditFragment)
    }

    override fun navigateToReaderFragment() {
        findNavController().navigate(R.id.action_feedFragment_to_readerFragment)
    }

    override fun navigateToReaderWebViewFragment(link: String?, enclosureLink: String?) {
        findNavController().navigate(
                R.id.action_feedFragment_to_readerWebViewFragment,
                bundle {
                    putString(ARG.LINK, link)
                    putString(ARG.ENCLOSURE, enclosureLink)
                }
        )
    }

    override fun navigateToFeedRuleFragment(feedId: Long?, regContent: String?, adFlag: Boolean?) {
        findNavController().navigate(R.id.action_global_feedRuleEditorDialog, bundle {
            feedId?.let { putLong(ARG.FEED_ID, feedId)}
            regContent?.let {  putString("regContext", regContent)}
            adFlag?.let {  putBoolean("adFlag",adFlag) }
        })
    }
}
